﻿using Shop.Collect;
using Shop.ExtendService;
using Shop.Model.Advert;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.Advert.Model;
using Shop.Service.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Service.lmpl
{
    internal class AdvertService : IAdvertService
    {
        private readonly IAdvertCollect _Service;
        private readonly IGoodsSkuCollect _Sku;
        private readonly IActivityCollect _Activity;
        private readonly IFileService _File;
        public AdvertService ( IAdvertCollect service,
            IGoodsSkuCollect sku,
            IActivityCollect activity,
            IFileService file )
        {
            this._File = file;
            this._Sku = sku;
            this._Activity = activity;
            this._Service = service;
        }

        public long Add ( AdvertAdd data )
        {
            AdvertAddArg add = data.ConvertMap<AdvertAdd, AdvertAddArg>();
            if ( add.PutInEnd == 0 )
            {
                add.PutInEnd = DateTime.MaxValue.ToLong();
            }
            long id = this._Service.Add(add);
            if ( data.FileId.HasValue )
            {
                this._File.Save(data.FileId.Value, id);
            }
            return id;
        }

        public void Delete ( long id )
        {
            DBAdvert advert = this._Service.Get(id);
            this._Service.Delete(advert);
            this._File.Drop(advert.ImgSrc);
        }

        public void Enable ( long id )
        {
            DBAdvert advert = this._Service.Get(id);
            this._Service.Enable(advert);
        }

        public AdvertDto Get ( long id )
        {
            DBAdvert advert = this._Service.Get(id);
            AdvertDto dto = advert.ConvertMap<DBAdvert, AdvertDto>();
            if ( dto.PutInEnd.HasValue && dto.PutInEnd.Value == DateTime.MaxValue )
            {
                dto.PutInEnd = null;
            }
            if ( dto.StopTime.HasValue && dto.StopTime.Value == DateTime.MaxValue )
            {
                dto.StopTime = null;
            }
            if ( dto.RelationType == AdvertRelationType.商品 && dto.RelationId != 0 )
            {
                dto.RelationTitle = this._Sku.GetSkuName(dto.RelationId);
            }
            else if ( dto.RelationType == AdvertRelationType.活动 && dto.RelationId != 0 )
            {
                dto.RelationTitle = this._Activity.GetActivityName(dto.RelationId);
            }
            return dto;
        }


        public PagingResult<AdvertBase> Query ( AdvertQueryParam query, IBasicPage paging )
        {
            DBAdvert[] list = this._Service.Query<DBAdvert>(query, paging, out int count);
            if ( list.IsNull() )
            {
                return new PagingResult<AdvertBase>(count);
            }
            Dictionary<long, string> skus = this._Sku.GetSkuName(list.Convert(c => c.RelationType == AdvertRelationType.商品 && c.RelationId != 0, c => c.RelationId));
            Dictionary<long, string> activitys = this._Activity.GetActivityName(list.Convert(c => c.RelationType == AdvertRelationType.活动 && c.RelationId != 0, c => c.RelationId));
            AdvertBase[] dto = list.ConvertMap<DBAdvert, AdvertBase>();
            dto.ForEach(c =>
            {
                if ( c.PutInEnd.HasValue && c.PutInEnd.Value == DateTime.MaxValue )
                {
                    c.PutInEnd = null;
                }
                if ( c.StopTime.HasValue && c.StopTime.Value == DateTime.MaxValue )
                {
                    c.StopTime = null;
                }
                if ( c.RelationType == AdvertRelationType.商品 && c.RelationId != 0 )
                {
                    c.RelationTitle = skus.GetValueOrDefault(c.RelationId);
                }
                else if ( c.RelationType == AdvertRelationType.活动 && c.RelationId != 0 )
                {
                    c.RelationTitle = activitys.GetValueOrDefault(c.RelationId);
                }
            });
            return new PagingResult<AdvertBase>(count, dto);
        }

        public bool Set ( long id, AdvertSet data )
        {
            DBAdvert advert = this._Service.Get(id);
            AdvertSetArg set = data.ConvertMap<AdvertSet, AdvertSetArg>();
            if ( set.PutInEnd == 0 )
            {
                set.PutInEnd = DateTime.MaxValue.ToLong();
            }
            set.StopTime = set.PutInEnd;
            set.ActualBegin = set.PutInBegin;
            string src = advert.ImgSrc;
            if ( this._Service.Set(advert, set) )
            {
                this._File.Sync(data.FileId, id, src);
                return true;
            }
            return false;
        }
        public void End ( long id )
        {
            DBAdvert advert = this._Service.Get(id);
            this._Service.End(advert);
        }

        public void Stop ( long id )
        {
            DBAdvert advert = this._Service.Get(id);
            this._Service.Enable(advert);
        }
    }
}
